import pygame
from Controls import *
from Constants import *


'''

check bullet hell menu traversing and next_selection reset
QUIT quits everything


possible issues with needing to reset next_selection? do so in Main?
can probably fix in update() by setting activemenu to next_selection

test options menus in update
may need game as a parameter?

think about multiple menus available at once (size, res, loc for each menu)
think about how level editor works

'''


class MenuHandler(object):
	'''Handles all menu object interactions.'''
	def __init__(self):
		'''Initializes the object.'''
		self.activemenu = MAIN_MENU
		self.next_selection = MAIN_MENU
		self.mode = 0
		self.chapter = 0
		self.level = 0

	def handleevent(self, event):
		'''Interprets the given event.'''
		if event.type == pygame.MOUSEMOTION:
			menus[self.activemenu].checkmouse(event.pos)
		elif event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == 1:
				controls["lmb"] = True
				menus[self.activemenu].checkmouse(event.pos)
			elif event.button == 3:
				controls["rmb"] = True
		elif event.type == pygame.MOUSEBUTTONUP:
			if event.button == 1:
				controls["lmb"] = False
				if menus[self.activemenu].checkmouse(event.pos):
					if menus[self.activemenu].highlight != 0:
						self.next_selection = menus[self.activemenu].nodes[menus[self.activemenu].highlight-1]
			elif event.button == 3:
				controls["rmb"] = False
		elif event.type == pygame.KEYDOWN:
			if event.key not in controls:
				return
			controls[event.key] = True
			if event.key == k_quit:
				self.next_selection = menus[self.activemenu].nodes[-1]
			elif event.key == k_pause:
				if self.activemenu == PAUSE_GAME:
					self.next_selection = START_GAME
					menus[self.activemenu].highlight = 1
			elif event.key == k_up:
				menus[self.activemenu].movehighlight(-1)
			elif event.key == k_down:
				menus[self.activemenu].movehighlight(1)
			elif event.key == k_action1 or event.key == k_action2:
				if menus[self.activemenu].highlight != 0:
					self.next_selection = menus[self.activemenu].nodes[menus[self.activemenu].highlight-1]
		elif event.type == pygame.KEYUP:
			if event.key not in controls:
				return
			controls[event.key] = False

	def update(self, pause):
		'''Special menu logic that takes place (such as changing game options).'''
		if self.next_selection != self.activemenu:
			if self.next_selection == QUIT or self.next_selection == RESET_GAME:
				menus[self.activemenu].highlight = 1

			menus[self.activemenu].update()

			if self.next_selection == MAIN_MENU:
				if pause:
					self.next_selection = PAUSE_GAME

			if self.next_selection == 50:
				config.edit(print_fps = not config.print_fps)

			if self.next_selection in menus:
				menus[self.activemenu].update()
				self.activemenu = self.next_selection

	def draw(self, surf):
		'''Draw the active menu and any extra objects.'''
		menus[self.activemenu].draw(surf)

		#extra stuff here
